package ANDROID_UF_CAMERA;

public class Example {
    private Camera camera;

    private void initCamera() {
        camera = Camera.open();
    }

    public boolean onKeyDown(final int keyCode, final KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
            if (camera != null) {
                final CameraPictureCallbackImpl callback = new CameraPictureCallbackImpl();
                camera.takePicture(null, null, callback);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private class CameraPictureCallbackImpl implements Camera.PictureCallback {
        private Bitmap bitmap;

        public void onPictureTaken(final byte[] bytes, final Camera camera) {
            camera.stopPreview();
            camera.release();
            bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
            if (bitmap == null) {
                camera.startPreview();
            }
        }

        public Bitmap getBitmap() {
            return bitmap;
        }
    }
}
